<template>
    <el-dialog class="content" :title="title" :modal-append-to-body="false" :visible.sync="isShow" width="30%"
        :lock-scroll="true" :close-on-click-modal="false">
        <template>
            <download-excel class="export-excel-wrapper " :data="config.data" :fields="config.json_fields" header=""
                :name="`${config.filename}.xls`">
                <el-button type="primary" @click="excelExport">导出excel</el-button>
            </download-excel>
        </template>
        <el-button type="primary" @click="csvExport" class="item">导出csv</el-button>
    </el-dialog>
</template>
<script>
export default {
    data() {
        return {
            isShow: false,
            title: "导出数据",
            config: {}
        }
    },
    props: {
        sensorInfo: {
            type: Object,
            default() {
                return null
            }
        },

    },
    methods: {
        downloadData(a) {

            this.config = a
            this.isShow = true
        },
        excelExport() {
            this.isShow = false
        },
        csvExport() {
            let { filename, data, json_fields } = this.config
            var dataKey = Object.values(json_fields)
            var title = Object.keys(json_fields)
            let str = []
            str.push(this.header + '\t\r\n')
            str.push(title.join(',') + '\r\n')
            for (let i = 0; i < data.length; i++) {
                let temp = []
                for (let j = 0; j < dataKey.length; j++) {
                    let a = data[i][dataKey[j]]
                    if (dataKey[j] == 'time')
                        a = a + '\t'
                    temp.push(a)
                }
                str.push(temp.join(',') + '\r\n')
            }
            const blob = new Blob(['\uFEFF' + str.join('')], {
                type: 'test/csv;charset=utf-8',
            })
            //导出
            const url = window.URL.createObjectURL(blob)
            const downloadLink = document.createElement('a')
            downloadLink.href = url
            //导出文件的文件名
            downloadLink.download = `${filename}.csv`
            downloadLink.click()
            window.URL.revokeObjectURL(url)
            this.isShow = false
        },
    },

}
</script>

<style lang="less" scoped>
.content {
    z-index: 9999;

    .item {
        margin-top: 14px;
    }
}
</style>
